package com.spring.utils.api_v4.dao;

import com.spring.utils.api_v4.pojo.*;
import com.spring.utils.api_v4.pojo.salary.task.DeptWageTask;
import com.spring.utils.api_v4.pojo.salary.task.EmployeeSalaryTask;
import com.spring.utils.api_v4.pojo.salary.task.WageTask;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 员工工资访问接口
 */
@Repository
@CacheConfig(cacheNames = "my_cache")
public interface IEmployeeSalaryTaskDao extends JpaRepository<EmployeeSalaryTask, String> {

    /**
     * 指定任务的员工工资信息
     */
    void deleteAllByTask(DeptWageTask task);

    /**
     * 获取部门员工绩效列表
     * @param task
     * @return
     */
    List<EmployeeSalaryTask> findAllByTask(DeptWageTask task);

    /**
     * 修改绩效工资金额
     * @param id
     * @param merit
     */
    @Modifying
    @Query(value = "update EmployeeSalaryTask set meritPay=?2 where id=?1")
    @CachePut(key = "#id")
    int updateMeritById(String id, double merit);

    /**
     * 根据工资任务查询
     * @param task
     * @return
     */
    @Cacheable
    List<EmployeeSalaryTask> findAllByTaskTask(WageTask task);

    /**
     * 工资任务求和
     * @param task
     * @return
     */
    @Query(value = "select sum(es.meritPay) from EmployeeSalaryTask as es where es.task.task=?1")
    double getTotalWageOfTask(WageTask task);

    /**
     * 部门工资求和
     * @param task
     * @return
     */
    @Query(value = "select sum(es.meritPay) from EmployeeSalaryTask as es where es.task=?1")
    double getTotalWageOfDept(DeptWageTask task);

    /**
     * 删除员工工资信息
     * @param user
     * @return
     */
    @Transactional
    List<EmployeeSalaryTask> deleteAllByUserInfo(UserInfo user);

    /**
     * 删除部门员工所有工资信息
     * @param dept
     */
    void deleteAllByUserInfoDepart(DeptInfo dept);

    /**
     * 统计部门任务中指定状态的员工任务数量
     * @param task
     * @param state
     * @return
     */
    int countByTaskAndState(DeptWageTask task, int state);

    /**
     * 判断指定任务下指定用户的工资信息是否存在
     * @param task
     * @param user
     * @return
     */
    boolean existsByTaskAndUserInfo(DeptWageTask task, UserInfo user);
}
